#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

/**
 * Initialize the program: create fifos if not existing
 */
int main()
{
	// Remove the lock file if it exists; may have remained from past abnormal termination
	struct stat file_stat;
	if(stat("chatlock", file_stat) >= 0) {
		printf ("Removing the lockfile...\n");
		unlink("chatlock");
	} else {
		printf ("Lockfile not existing; nothing to do...\n");
	}

	// Create fifo1 if it doesn't exist
	int fifo1 = mkfifo("fifo_client1", 0666);
	if (fifo1 == -1) {
		if (errno == EEXIST) {
			printf ("FIFO exists nothing to do...\n");
		} else {
			perror("mkfifo");
		}
	}
	// Create fifo2 if it doesn't exist
	int fifo2 = mkfifo("fifo_client2", 0666);
	if (fifo2 == -1) {
		if (errno == EEXIST) {
			printf ("FIFO exists nothing to do...\n");
		} else {
			perror("mkfifo");
		}
	}

	return 0;
}

